clear variables;close all;
%% The variables used here are constant over IPER, but change depending setup configuration, stored in Zoetwatervat.xsl/SETUP. 
% Setup vars will be used in mf?adapt
% Result vars will be retreived from mf?analyze

%% load setup variables
[SETUPparnames,SETUPparvals]=getExcelData('Zoetwatervat.xls','SETUP','H');  
SERIES=round(SETUPparvals(:,strmatchi('SERIES',SETUPparnames)));
SETUP_XWALL=round(SETUPparvals(:,strmatchi('SETUP_XWALL',SETUPparnames)));
SETUP_ZWALL=round(SETUPparvals(:,strmatchi('SETUP_ZWALL',SETUPparnames)));
MULTIMILK=round(SETUPparvals(:,strmatchi('MULTIMILK',SETUPparnames)))./10;
ANALYZEPRETTY=round(SETUPparvals(:,strmatchi('ANALYZEPRETTY',SETUPparnames)));
FSSE=round(SETUPparvals(:,strmatchi('FSSE',SETUPparnames)));
POREFF=round(SETUPparvals(:,strmatchi('POREFF',SETUPparnames)));
CONDUC=round(SETUPparvals(:,strmatchi('CONDUC',SETUPparnames)));
REUSECONC=round(SETUPparvals(:,strmatchi('REUSECONC',SETUPparnames)));
NSetup=length(SERIES);

for i=1:length(REUSECONC);
    if i~=length(REUSECONC);  % determines wether the movie should stop when intake is saline
        REUSENEXTCONC(i)=REUSECONC(i+1);
    else
        REUSENEXTCONC(i)=0;
    end
end

save setup SERIES SETUP_XWALL SETUP_ZWALL MULTIMILK ANALYZEPRETTY FSSE POREFF CONDUC REUSECONC REUSENEXTCONC

%% Preallocate results
ConcentrationWellSaved=zeros(NSetup,1);  
QInjectedSaved=zeros(NSetup,1);
QRetreivedSaved=zeros(NSetup,1);
RESaved=zeros(NSetup,1);
SizeTooSmallSaved=zeros(NSetup,1);
MaxQuantitySaved=zeros(NSetup,1);
save results ConcentrationWellSaved QInjectedSaved QRetreivedSaved RESaved SizeTooSmallSaved MaxQuantitySaved;
xlswrite('Results.xls', [SERIES,SETUP_XWALL,SETUP_ZWALL,MULTIMILK,POREFF,CONDUC,REUSECONC,ConcentrationWellSaved,QInjectedSaved,QRetreivedSaved,RESaved,SizeTooSmallSaved,MaxQuantitySaved], 'RESULTS', 'A2');

%% Loop
for i=1:NSetup;
    CurNum=i;
    save('CurrentSeriesNumber.mat','CurNum');
    fprintf('this is the %d th process',CurNum);
    echo off
    mf_setup;
    clear all;
    load setup;
    load CurrentSeriesNumber.mat;
    
    if ANALYZEPRETTY(CurNum)==1,
        mf_analyzepretty;
    else
        mf_analyze;
    end
    echo on
end

%% Save results
load setup
load results
xlswrite('Results.xls', [SERIES,SETUP_XWALL,SETUP_ZWALL,MULTIMILK,POREFF,CONDUC,REUSECONC,ConcentrationWellSaved,QInjectedSaved,QRetreivedSaved,RESaved,SizeTooSmallSaved,MaxQuantitySaved], 'RESULTS', 'A2');
load handel;
sound(y,Fs);
error('Thank you for running me, results are printed to the Results excel.\n');